using MicroMsg.Common.Event;
using MicroMsg.Common.Utils;
using Microsoft.Phone.Net.NetworkInformation;
using System;
using System.Net.Sockets;
namespace MicroMsg.Network
{
	public class NetworkDeviceWatcher
	{
		private const string TAG = "NetworkDeviceWatcher";
		private const string SUPPORT_MIN_VER = "7.10.7720";
		private static NetworkInterfaceInfo mCurrentNetworkInfo;
		private static string mLastConnectedNetName;
		private static double mLastUpdateTime;
		private static bool mIsNetworkAvailable;
		public static void initWatcher()
		{
			DeviceNetworkInformation.add_NetworkAvailabilityChanged(new EventHandler<NetworkNotificationEventArgs>(NetworkDeviceWatcher.onNetChangeDetected));
		}
		private static void onNetChangeDetected(object sender, NetworkNotificationEventArgs e)
		{
			switch (e.get_NotificationType())
			{
			case 0:
				Log.smethod_4("Network", "connected to " + e.get_NetworkInterface().get_InterfaceName() + ":" + e.get_NetworkInterface().get_InterfaceType().ToString());
				LongConnector.setAvailable(true);
				EventCenter.postEvent(EventConst.ON_NET_DEVICE_CONNECTED, null, null);
				return;
			case 1:
				Log.smethod_4("Network", "disconnected from " + e.get_NetworkInterface().get_InterfaceName() + ":" + e.get_NetworkInterface().get_InterfaceType().ToString());
				return;
			default:
				Log.smethod_4("Network", "unknown change with " + e.get_NetworkInterface().get_InterfaceName() + ":" + e.get_NetworkInterface().get_InterfaceType().ToString());
				return;
			}
		}
		private static NetworkInterfaceInfo getCurrentNetworkInfo()
		{
			try
			{
				Socket rawSocket = LongConnector.getRawSocket();
				NetworkInterfaceInfo result;
				if (rawSocket == null)
				{
					result = null;
					return result;
				}
				string text = Environment.get_OSVersion().get_Version().ToString();
				if (string.Compare(text, "7.10.7720") < 0)
				{
					result = null;
					return result;
				}
				result = NetInterfaceInfo.getNetworkInfo(rawSocket);
				return result;
			}
			catch (Exception ex)
			{
				Log.smethod_1("NetworkDeviceWatcher", ex.get_Message());
			}
			return null;
		}
		private static bool isCurrentSubType(NetworkInterfaceSubType type)
		{
			return NetworkDeviceWatcher.mCurrentNetworkInfo != null && NetworkDeviceWatcher.mCurrentNetworkInfo.get_InterfaceSubtype() == type;
		}
		private static bool isCurrentType(NetworkInterfaceType type)
		{
			return NetworkDeviceWatcher.mCurrentNetworkInfo != null && NetworkDeviceWatcher.mCurrentNetworkInfo.get_InterfaceType() == type;
		}
		public static void onConnnectClosed()
		{
			NetworkDeviceWatcher.mCurrentNetworkInfo = null;
		}
		public static void onConnected()
		{
			NetworkDeviceWatcher.mCurrentNetworkInfo = NetworkDeviceWatcher.getCurrentNetworkInfo();
			if (NetworkDeviceWatcher.mCurrentNetworkInfo == null)
			{
				return;
			}
			NetworkDeviceWatcher.printfInfo();
			if (!string.IsNullOrEmpty(NetworkDeviceWatcher.mLastConnectedNetName) && !NetworkDeviceWatcher.mLastConnectedNetName.Equals(NetworkDeviceWatcher.mCurrentNetworkInfo.get_InterfaceName()))
			{
				Log.smethod_4("Network", "network interface changed, from " + NetworkDeviceWatcher.mLastConnectedNetName + " to " + NetworkDeviceWatcher.mCurrentNetworkInfo.get_InterfaceName());
			}
			NetworkDeviceWatcher.mLastConnectedNetName = NetworkDeviceWatcher.mCurrentNetworkInfo.get_InterfaceName();
		}
		public static void printfInfo()
		{
			if (NetworkDeviceWatcher.mCurrentNetworkInfo == null)
			{
				return;
			}
			Log.smethod_4("Network", string.Concat(new object[]
			{
				"current network name = ",
				NetworkDeviceWatcher.mCurrentNetworkInfo.get_InterfaceName(),
				", type = ",
				NetworkDeviceWatcher.mCurrentNetworkInfo.get_InterfaceType(),
				", subtype = ",
				NetworkDeviceWatcher.mCurrentNetworkInfo.get_InterfaceSubtype(),
				", bandwidth = ",
				NetworkDeviceWatcher.mCurrentNetworkInfo.get_Bandwidth(),
				", state = ",
				NetworkDeviceWatcher.mCurrentNetworkInfo.get_InterfaceState()
			}));
		}
		public static bool isNetworkAvailable()
		{
			double now = SessionPack.getNow();
			if (now > NetworkDeviceWatcher.mLastUpdateTime + 2000.0)
			{
				NetworkDeviceWatcher.mLastUpdateTime = now;
				NetworkDeviceWatcher.mIsNetworkAvailable = DeviceNetworkInformation.get_IsNetworkAvailable();
			}
			return NetworkDeviceWatcher.mIsNetworkAvailable;
		}
		public static bool isWifiAvailable()
		{
			return DeviceNetworkInformation.get_IsWiFiEnabled();
		}
		public static bool IsCellularDataEnabled()
		{
			return DeviceNetworkInformation.get_IsCellularDataEnabled();
		}
		public static bool isCurrentWifi()
		{
			return NetworkDeviceWatcher.isCurrentSubType(9) || NetworkDeviceWatcher.isCurrentType(6);
		}
		public static bool isCurrentCell3G()
		{
			return NetworkDeviceWatcher.isCurrentSubType(5);
		}
		public static bool isCurrentCell2G()
		{
			return NetworkDeviceWatcher.isCurrentSubType(1) || NetworkDeviceWatcher.isCurrentSubType(4);
		}
		public static NetworkInterfaceSubType getCurrentNetworkType()
		{
			if (NetworkDeviceWatcher.mCurrentNetworkInfo == null)
			{
				return 0;
			}
			return NetworkDeviceWatcher.mCurrentNetworkInfo.get_InterfaceSubtype();
		}
	}
}
